package com.jb.web.bean.custom.renderer;

import com.jb.faces.component.UIComponent;
import com.jb.framework.datasource.TableModel;
import com.jb.web.bean.FormField;
import com.jb.web.bean.Image;
import com.jb.web.bean.ListBox;
import com.jb.web.context.WebContext;
import com.jb.web.renderer.FormItemRenderer;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;


public class ShuttleRenderer extends FormItemRenderer {
    TableModel tableData ;
    public StringBuffer render(WebContext ctx, UIComponent itemToRender) {
        ListBox formField = (ListBox) itemToRender;
        String widthStr = formField.getWidth();
        
        if(widthStr!=null&&widthStr.length()>0)
            widthStr = widthStr.substring(0, widthStr.length()-2);
        try {
            listWidth = Integer.parseInt(widthStr)/2 - 12;
        }catch (Exception e) {
            listWidth = 160;
        }

        String heightStr = formField.getHeight();
        if(heightStr!=null&&heightStr.length()>0)
            heightStr = heightStr.substring(0, heightStr.length()-2);
        try {
            listHeight = Integer.parseInt(heightStr) - 6;
        }catch (Exception e) {
            listHeight = 80;
        }

        StringBuffer html = new StringBuffer();
        html.append("<div ")
        .append(getEditScript(ctx, formField))
        .append(addTagProperty("style", formField.getStyle(ctx), null))
        .append(addTagProperty("class", formField.getStyleClass(), null))
            .append(">");
        

        tableData = optionData(ctx, formField, formField.getValueOptions());
        if (isVisible(ctx, formField)) {
            html.append("<table style=\"width:100%;height:100%\"><tr> <td>")
            .append(leftField(ctx, formField));
            html.append("</td><td style=\"width:12px;\" align=\"center\"> ")
            .append(buttons(ctx, formField));
            html.append("</td><td> ")
            .append(rightField(ctx, formField));
            html.append("</td></tr></table> ");
        }
        html.append(_formField(formField));
        return html.append("</div>");
    }
    int listWidth=160;
    int listHeight=80;
    private String buttons(WebContext ctx, FormField formItem) {
        ListBox select = (ListBox) formItem;

        String divId= formItem.getClientId()+"div";
        String leftId= formItem.getClientId()+"left";
        String rightId= formItem.getClientId()+"right";
        StringBuffer html = new StringBuffer("<div ");
        html.append(addTagProperty("id", divId, null))
        .append(addTagProperty("class", "jb_shuttle_buttons", null))
            .append(">");
        
        Image imgLeft = new Image();
        Image imgRight = new Image();
        
        imgLeft.setDisplayStyle("block");
        imgRight.setDisplayStyle("block");

        imgRight.setLocation("resources/images/palette/rightArrowActive.gif");
        imgLeft.setLocation("resources/images/palette/leftArrowActive.gif");
        imgRight.setOnClick("JBShuttle.moveOptions('" + leftId + "', '" + rightId + "', '" + select.getClientId() +
            "');");
        imgLeft.setOnClick("JBShuttle.moveOptions('" + rightId + "', '" + leftId + "', '" + select.getClientId() +
            "');");
        
        imgLeft.setDestination("javascript:void(0);");
        imgRight.setDestination("javascript:void(0);");
        html.append(imgLeft.render(ctx));
        html.append(imgRight.render(ctx));
        
        html.append("</div>");
        return html.toString();
    }

    private String leftField(WebContext ctx, FormField formItem) {
        ListBox select = (ListBox) formItem;
        String leftId= formItem.getClientId()+"left";
        String rightId= formItem.getClientId()+"right";
        StringBuffer html = new StringBuffer("<SELECT ");
        html.append(addTagProperty(" multiple", "multiple", null));
        html.append(addTagProperty("class", "jb_shuttle_left", null))
            .append(addTagProperty("style", "height:"+listHeight+"px;width:"+listWidth+"px;", null))
        .append(addTagProperty("id", leftId, null));

        if (ctx.isRunTime()) {
            html.append(addTagProperty("disabled", select.isEnabled() ? "" : "disabled", null));
        }
        
        html.append(addTagProperty("ondblclick", "JBShuttle.moveOptions('" 
                                                 +leftId+"', '"
                                                 +rightId+"', '"
                                                 +formItem.getClientId()+"');", null))
            .append(">");

        Collection list = select.getSelected();
        if (tableData != null) {
            for (int i = 0; i < tableData.getMaxValue(); i++) {
                Iterator row = tableData.getItem(i);
                String dbName =  row.next().toString();
                String dbValue = row.next().toString();
                if (!list.contains(dbValue)) {
                    html.append("<OPTION value=\"" + dbValue + "\">" + dbName + "</OPTION>\n");
                }
            }
        }
        html.append("</SELECT>");
        return html.toString();
    }
    String[] options;
    String[] values;
    private String rightField(WebContext ctx, FormField formItem) {
        ListBox select = (ListBox) formItem;
        String leftId= formItem.getClientId()+"left";
        String rightId= formItem.getClientId()+"right";
        StringBuffer html = new StringBuffer("<SELECT ");

        html.append(addTagProperty(" multiple", "multiple", null));
        html.append(addTagProperty("class", "jb_shuttle_right", null))
        .append(addTagProperty("style", "height:"+listHeight+"px;width:"+listWidth+"px;", null))
        .append(addTagProperty("id", rightId, null));

        if (ctx.isRunTime()) {
            html.append(addTagProperty("disabled", select.isEnabled() ? "" : "disabled", null));
        }
        
        html.append(addTagProperty("ondblclick", "JBShuttle.moveOptions('"
            +rightId+"', '"
            +leftId+"', '"
            +formItem.getClientId()+"');", null))
            .append(">\n");

        if (tableData != null) {
            List list = select.getSelected();
            options=new String[list.size()];
            values=new String[list.size()];
            for (int i = 0; i < tableData.getMaxValue(); i++) {
                Iterator row = tableData.getItem(i);
                String dbName =  row.next().toString();
                String dbValue = row.next().toString();
                int j = list.indexOf(dbValue);
                if (j!=-1) {
                    options[j] =  dbName ;
                    values[j] =  dbValue ;
                } 
            }
            for(int i=0;i<options.length;i++) {
                html.append("<OPTION value=\"" + values[i] + "\" >" + options[i] + "</OPTION>\n");
            }
        }
        html.append("</SELECT>");
        return html.toString();
    }

    private String _formField(FormField formItem) {
        ListBox select = (ListBox) formItem;
        String formFieldId= formItem.getClientId()+"f";
        StringBuffer html = new StringBuffer("<SELECT ");
        html.append(addTagProperty("multiple", "multiple", null));
        html.append(addTagProperty("style", "display:none;", null));
        html.append(addTagProperty("name", select.getFieldName(), null));
        html.append(addTagProperty("id", formFieldId, null));   
        html.append(">");
        if(options!=null)
            for(int i=0;i<options.length;i++) {
                html.append("<OPTION selected=\"true\" value=\"" + values[i] + "\" >" + options[i] + "</OPTION>\n");
            }
        html.append("</SELECT>");
        return html.toString();
    }
}
